package kr.ac.hufs.ice.cap.network;


import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import kr.ac.hufs.ice.cap.network.NetworkData;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

public class NetworkThread_NonMember extends Thread {
	
	NetworkData nd;
	Handler mHandler2;
	int commandState; 
	public String[] returnMsgArr;
	
	private final String SERVER_ADDRESS = "http://220.67.124.69/nonmember/visit_request.php"; 
	
	public static final int REGISTER = 0;
	
	public NetworkThread_NonMember(NetworkData nd, Handler mHandler2) {
		this.nd = nd;
		this.mHandler2 = mHandler2;
	}
	
	public void setCommand(int command) {
		this.commandState = command;
	}
	
	public NetworkThread_NonMember setRCommand(int command) {
		this.commandState = command;
		return this;
	}
	
	public void run() {
		Looper.prepare();
		if(commandState == REGISTER) {
			register();	
		}
		
		Looper.loop();
	}
	
	private void register() {
		
		String NonMName = nd.getNonMName();
		String NonMPhoneNum = nd.getNonMPhoneNum();
		String NonMCompany = nd.getNonMCompany();
		String NonMSchool = nd.getNonMSchool();
		String NonMPurpose = nd.getNonMPurpose();
		String NonMDate = nd.getNonMDate();
		
		PageStreamer ps = new PageStreamer();
		ps.setRootURL(SERVER_ADDRESS);

		ps.add("NonMName", NonMName);
		ps.add("NonMPhoneNum", NonMPhoneNum);
		ps.add("NonMCompany", NonMCompany);
		ps.add("NonMSchool", NonMSchool);
		ps.add("NonMPurpose", NonMPurpose);
		ps.add("VisitDate", NonMDate);
		
		String returnMsg = ps.getPageStream();
		Message msg = new Message();
		msg.what = NetworkThread.REGISTER;
		if(returnMsg.contains("success")) {
			msg.obj = "success";
			mHandler2.sendMessage(msg);
		}else {
			msg.obj = "fail";
			mHandler2.sendMessage(msg);
		}
	}
}
